package com.wifi.eshipewifi;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.wifi.BaseApplication;
import com.wifi.imp.MessageListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class SimSettingActivity extends AppCompatActivity implements View.OnClickListener,MessageListener{
    ImageButton ib_back;
    TextView tv_include_tittle;
    EditText et_phone_num;
    LinearLayout ll_sim_place;
    RelativeLayout rl_phone_sure;
    BaseApplication application;
    private String provinceName;//省的名字
    int selectPosition=0;
    SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sim);
        application = (BaseApplication) getApplication();
        sp= getSharedPreferences("package", Activity.MODE_PRIVATE);

        ll_sim_place = (LinearLayout) findViewById(R.id.ll_sim_place);
        rl_phone_sure = (RelativeLayout) findViewById(R.id.rl_phone_sure);
        tv_include_tittle = (TextView) findViewById(R.id.tv_include_tittle);
        et_phone_num = (EditText) findViewById(R.id.et_phone_num);
        ib_back = (ImageButton) findViewById(R.id.ib_back);
        tv_include_tittle.setText("SIM卡设置");
        ib_back.setOnClickListener(this);
        rl_phone_sure.setOnClickListener(this);
//        ll_sim_place.setOnClickListener(this);

        spinner_sheng = (Spinner) findViewById(R.id.spinner_sheng);
        //省
        spinner_sheng.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                provinceName = parent.getSelectedItem() + "";//获取点击列表spinner item的省名字
                SharedPreferences.Editor edit = sp.edit();
                edit.putString("sim2_place", provinceName);
                edit.commit();
                selectPosition =position;
                application.sendMsg("$set_location;"+selectPosition+";");
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

    }
    String sim;
    @Override
    protected void onResume() {//每个activity都要加这一句
        super.onResume();
        if(application==null)
            application = (BaseApplication) getApplication();
        application.setMsgListener(this);
        sim = getIntent().getStringExtra("sim");
        if(sim.indexOf("sim1")!=-1){
            ll_sim_place.setVisibility(View.GONE);
        }else {
            ll_sim_place.setVisibility(View.VISIBLE);
        }
        initDatas();
        application.sendMsg("$show_location;"+sim+";");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ib_back:
                this.finish();
                break;
            case R.id.rl_phone_sure:
                String et_phone_num1 = et_phone_num.getText().toString().trim();
                if (et_phone_num1 == null) {
                    application.makeToast(SimSettingActivity.this,"手机号不得为空",true);
                } else if (et_phone_num1.length() < 11) {
                    application.makeToast(SimSettingActivity.this,"请填写正确的手机号码",true);
                }else {
//                    $set_cellphone;sim1;13606543210;
                    application.sendMsg("$set_cellphone;" + sim + ";" + et_phone_num1 + ";");
                }
                break;
        }
    }
    private Spinner spinner_sheng;//省
    private ArrayAdapter<String> provinceAdapter;//省份数据适配器
    private String[] allProv={"全国","北京","天津","河北","山西","内蒙古","辽宁","吉林"
                                ,"黑龙江","上海","江苏","浙江","安徽","福建","江西"
                                ,"山东","河南","湖北","湖南","广东","广西","海南","重庆"
                                ,"四川","贵州","云南","西藏"
                                ,"陕西","甘肃","青海","宁夏","新疆","香港","澳门","台湾"};//所有的省
    //初始化省市区数据
    private void initDatas() {
        int selectPosition = 0;//有数据传入时
        /**
         * 设置省市区的适配器，进行动态设置
         */
        provinceAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);//系统默认的
        for (int i = 0; i < allProv.length; i++) {
            //给spinner省赋值,设置默认值
            selectPosition = i;
            provinceAdapter.add(allProv[i]);//添加每一个省
        }
        provinceAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//按下的效果
        spinner_sheng.setAdapter(provinceAdapter);
        spinner_sheng.setSelection(selectPosition);//设置选中的省，默认

    }



    String TAG="SimSettingActivity";
    @Override
    public void MessageGetListener(String msg) {
        if (msg.trim().length() > 0) {
            Log.i(TAG, "mhandler接收到obj=" + msg);
            String[] s1=msg.split(";");
            if(s1[0].indexOf("show_location")!=-1){
// $show_location;not_set;1360000000\r\n"--没有设置归属地 $show_location;37;\r\n" --数字表示归属地
                if(s1[1].indexOf("not_set")!=-1){
                    spinner_sheng.setSelection(0);//默认全国卡
                }else {
                    Integer integer = Integer.valueOf(s1[1]);
                    spinner_sheng.setSelection(integer);
                }

                if(s1[2].indexOf("not_set")!=-1){
                    et_phone_num.setText("");
                }else {
                    et_phone_num.setText(s1[2]);
                }

            }else if(s1[0].indexOf("set_location_done")!=-1){
// $show_location;not_set;\r\n"--没有设置归属地 $show_location;37;\r\n" --数字表示归属地
                application.makeToast(SimSettingActivity.this,"修改归属地成功",true);
            }else if(s1[0].indexOf("set_cellphone")!=-1){
// $show_location;not_set;\r\n"--没有设置归属地 $show_location;37;\r\n" --数字表示归属地
                application.makeToast(SimSettingActivity.this,"成功设置手机号",true);
            }
        }
    }
}
